﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapICC_STDMACHINE(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveICC_STDMACHINE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            Result = QueryICC_STDMACHINE(dwData);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapICC_STDMACHINE(ref dwLocal, dwData);

            if (!flgExist)
            {
                dwLocal["ROWNID"] = GetSeq();
                InsertICC_STDMACHINE(dwLocal, 1);
            }
            UpdateICC_STDMACHINE(dwLocal);
        }
        public void InsertICC_STDMACHINE(DataRow dwData, int option=1)
        {
             OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "INSERT INTO ICC_STDMACHINE (MACHINEID,AMKSTKRS,AMKST,AMTYP,MTYPE,AMVERFKZ,SMTYP) SELECT DISTINCT 'M_'||RTRIM(AMHERST) AS MACHINEID, AMKSTKRS,AMKST,AMTYP,AMKSTKRS||AMKST||AMTYP,AMVERFKZ,RTRIM(AMHERST) AS SMTYP  FROM AMSTAMM  GROUP BY AMKSTKRS,AMKST,AMTYP,AMVERFKZ,AMHERST ORDER BY 5";
                    /*
                    or1.Parameters.Add("AMMANDANT", OracleType.NVarChar).Value = dwData["AMMANDANT"].ToString();
                    or1.Parameters.Add("AMKSTKRS", OracleType.NVarChar).Value = dwData["AMKSTKRS"].ToString();
                    or1.Parameters.Add("AMKST", OracleType.NVarChar).Value = dwData["AMKST"].ToString();*/
                    break;
                default:
                    break;
            }            
            Connect2Proxy(or1);
        }
        public void DeleteICC_STDMACHINE(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM ICC_STDMACHINE";
                    break;
                default: 
                    break;
            }
            Connect2Proxy(or1);
        }
        public DataSet QueryICC_STDMACHINE(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1: 
                    or1.CommandText = "SELECT * FROM ICC_STDMACHINE "
                        + " WHERE  AMKSTKRS=:AMKSTKRS AND AMKST=:AMKST " ; 
                    or1.Parameters.Add("AMKSTKRS", OracleType.NVarChar).Value = dwData["AMKSTKRS"].ToString();
                    or1.Parameters.Add("AMKST", OracleType.NVarChar).Value = dwData["AMKST"].ToString(); 
                    break; 
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "ICC_STDMACHINE";
            Return = Result;
            return Return;
        }
        public void UpdateICC_STDMACHINE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            #region====Command====
            or1.CommandText = "UPDATE ICC_STDMACHINE SET "
                + "GABBS=:GABBS,"
                + "EN=:EN,"
                + "SCN=:SCN "
                + " WHERE  "
                + "    ROWNID=:ROWNID ";
            #endregion
            #region====Parameters==== 
            or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString(); 
            or1.Parameters.Add("GABBS", OracleType.NVarChar).Value = dwData["GABBS"].ToString();
            or1.Parameters.Add("EN", OracleType.NVarChar).Value = dwData["EN"].ToString();
            or1.Parameters.Add("SCN", OracleType.NVarChar).Value = dwData["SCN"].ToString();
            #endregion
            Connect2Proxy(or1);
        }
    }
}
